<template>
	<view>
		<view>
			<!-- 左侧通讯录 -->
			<scroll-view class="scroll-container" scroll-y="true" :scroll-into-view="toView" @scroll="scrollLeft"
				scroll-with-animation="true">
				<view class="common-text-brand-key">常用品牌</view>
				<scroll-view class="scroll-view-class" :scroll-x="true">
					<view class="scroll-view-class-box">
						<view v-for="(commonItem,index) in commonBrands" :key="index">
							<view class="common-box-brand-view-class" @click="commonBrands_click_item(commonItem)">
								<!-- <image class="common-image-brand-view-class" src="@/static/aodi.png" mode="widthFix"></image> -->
								<image class="common-image-brand-view-class" :src="commonItem.cover_url"
									mode="widthFix"></image>
								<view class="name-text">{{commonItem.name}}</view>
							</view>
						</view>
					</view>
				</scroll-view>

				<view v-for="(keyItem, index1) in brandDataLists" :key="index1" :id="keyItem.key">
					<view class="commonScroll">{{keyItem.key}}</view>
					<view v-for="(brandItem, index2) in keyItem.value" :key="index2">
						<view class="contact-container" @click="brands_click_item(brandItem)">
							<view
								style="width: 110rpx;height: 70rpx; display: flex;align-items: center;justify-content: center;">
								<image class="image-view" :src="brandItem.cover_url" mode="heightFix" />
							</view>
							<view class="name-text text-margin-left12">{{ brandItem.name }}</view>
						</view>
					</view>
				</view>
			</scroll-view>

			<!-- 右侧字母导航条 -->
			<view class="letter-nav">
				<view :class="temp==index?'right-view-text-select':'right-view-text'"
					v-for="(item, index) in brandDataLists" :key="index" @click="toSelectIndex(item.key,index)">
					{{item.key}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getCommonBrandsList,
		getBrandsList,
		getUsedSearchBrandList
	} from '@/api/brand.js'
	export default {
		data() {
			return {
				temp: 0,
				toView: '',
				commonBrands: [],
				brandDataLists: [],
				brand_type: 'search',
				users_id: '',

				topList: [],
				status:'3'
			}
		},
		onPageScroll(e) {
			console.log("滚动距离为：" + e.scrollTop);
		},

		onLoad(option) {
			this.$utils.log("打印 选择品牌 页面 传参数  "  + JSON.stringify(option))
			var that = this
			const value = uni.getStorageSync('login')
			if (value) {
				this.users_id = value.id
			}

			//判断是新车还是二手车
			if (option.newCar) {
				if (option.brand_type == 'search') { //判断是寻车还是发布二手车
					this.status = 2 + ''
				} else {
					this.status = 1 + ''
				}
			} else {
				if (option.brand_type == 'search') { //判断是寻车还是发布二手车
					this.status= 4 + ''
				} else {
					this.status = 3 + ''
				}
			}

			getCommonBrandsList(this.users_id, this.status).then(res => {
				that.$utils.log("打印获取到的网络常用品牌 " + JSON.stringify(res))
				that.commonBrands = res.data
			})
			if (option.brand_type == 'search') {
				this.brand_type = 'search'
				getUsedSearchBrandList().then(res => {
					// that.$utils.log("打印获取到的网络品牌列表 " + JSON.stringify(res))
					that.brandDataLists = res.data
				})
			} else {
				this.brand_type = 'secondhand'
				getBrandsList(0).then(res => {
					// that.$utils.log("打印获取到的网络品牌列表 " + JSON.stringify(res))
					that.brandDataLists = res.data
				})
			}
		},
		onReady() {
			setTimeout(() => {
				this.getNodesInfo()
			}, 2000)
		},
		methods: {
			getNodesInfo() {
				const query = uni.createSelectorQuery().in(this)
				query.selectAll('.commonScroll').boundingClientRect().exec(data => {
					// console.log(data);
					let res = data[0];
					let rel = [];
					res.map(item => {
						rel.push(item.top);
					});
					this.topList = rel;
				});
			},
			scrollLeft(e) {
				let scrollTop = e.target.scrollTop;
				for (let i = 0; i < this.topList.length; i++) {
					let h1 = this.topList[i];
					let h2 = this.topList[i + 1];
					if (scrollTop >= h1 && scrollTop < h2) {
						this.temp = i;
					}
				}
			},
			toSelectIndex(item, index) {
				this.toView = item
				this.temp = index
			},
			brands_click_item(item) {
				this.$utils.log(JSON.stringify(item))
				uni.navigateTo({
					url: '/pages/home/selectBrand/selectBrandName/selectBrandName?name=' + item.name +
						"&brand_id=" + item.id + "&brand_type=" + this.brand_type
				})
			},
			commonBrands_click_item(item) {
				uni.navigateTo({
					url: '/pages/home/selectBrand/selectBrandName/selectBrandName?name=' + item.name +
						"&brand_id=" + item.id + "&brand_type=" + this.brand_type
				})
			}

		}
	}
</script>

<style>
	.scroll-view-class {
		white-space: nowrap;
		background-color: white;
		height: auto;
	}

	.scroll-view-class-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		height: auto;
	}

	.content-item-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.scroll-container {
		height: 100vh;
	}

	.letter-nav {
		display: flex;
		position: fixed;
		right: 20px;
		top: 0;
		height: 100%;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.contact-container {
		display: flex;
		align-items: center;
		background-color: #fff;
		padding: 10rpx 0;
	}

	.image-view {
		height: 52rpx;
		background: #FFFFFF;
		border-radius: 22rpx;
	}

	.common-image-brand-view-class {
		width: 60rpx;
		min-height: 30rpx;
		height: auto;
		background: #FFFFFF;
		opacity: 1;
		border-radius: 22rpx;
		margin: 5rpx;
	}

	.common-text-brand-key {
		font-size: 32rpx;
		font-family: DIN Alternate;
		font-weight: bold;
		color: #1C2125;
		opacity: 1;
		padding: 8rpx 0px 6rpx 28rpx;
		background-color: #F4F4F4;
	}

	.commonScroll {
		font-size: 32rpx;
		font-family: DIN Alternate;
		font-weight: bold;
		color: #1C2125;
		opacity: 1;
		padding: 8rpx 0px 6rpx 28rpx;
		background-color: #F4F4F4;
	}

	.name-text {
		font-size: 28rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #000026;
	}

	.pad28 {
		padding: 18rpx 28rpx;
	}

	.right-view-text {
		font-size: 32rpx;
		font-family: DIN Alternate;
		font-weight: bold;
		color: #999999;
		padding: 4rpx;
	}

	.right-view-text-select {
		font-size: 32rpx;
		font-family: DIN Alternate;
		font-weight: bold;
		color: #0E59EA;
		padding: 4rpx;
	}

	.text-margin-left12 {
		margin-left: 12rpx;
	}

	.common-box-brand-view-class {
		height: auto;
		width: auto;
		text-align: center;
		margin: 20rpx 0rpx 20rpx 30rpx;
	}
</style>